﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FDSimpleModelGenerator.Common
{
    /// <summary>
    /// 表相关格式化操作
    /// </summary>
    public class TableFormatter
    {
        /// <summary>
        /// 格式化类名
        /// </summary>
        /// <param name="className">类名</param>
        /// <param name="characterFormat">首字母格式</param>
        /// <returns></returns>
        public static string FormatClassName(string className, CharacterFormat characterFormat = CharacterFormat.FirstUpper)
        {
            return FormatWord(className, characterFormat);
        }

        /// <summary>
        /// 格式化单词首字母大写
        /// </summary>
        /// <param name="className"></param>
        /// <returns></returns>
        public static string FormatFirstCharacterUpper(string className)
        {
            if (className.IsNullOrEmpty())
            {
                return className;
            }

            return $"{className.Substring(0, 1).ToUpper()}{className.Substring(1)}";
        }

        /// <summary>
        /// 格式化单词首字母小写
        /// </summary>
        /// <param name="className"></param>
        /// <returns></returns>
        public static string FormatFirstCharacterLower(string className)
        {
            if (className.IsNullOrEmpty())
            {
                return className;
            }

            return $"{className.Substring(0, 1).ToLower()}{className.Substring(1)}";
        }

        /// <summary>
        /// 将含下划线的单词组合成骆锋格式
        /// </summary>
        /// <param name="word"></param>
        /// <param name="characterFormat"></param>
        /// <returns></returns>
        public static string FormatWord(string word, CharacterFormat characterFormat = CharacterFormat.FirstUpper)
        {
            var words = word.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
            var sb = new StringBuilder();
            for (var i = 0; i < words.Length; ++i)
            {
                var w = words[i];
                if (i == 0)
                {
                    sb.Append(characterFormat == CharacterFormat.FirstUpper ? FormatFirstCharacterUpper(w) : FormatFirstCharacterLower(w));
                }
                else
                {
                    sb.Append(FormatFirstCharacterUpper(w));
                }
            }

            return sb.ToString();
        }

        /// <summary>
        /// 格式化类描述
        /// </summary>
        /// <param name="classDescription"></param>
        /// <returns></returns>
        public static string FormatClassDescription(string classDescription)
        {
            return
                $"{classDescription}\r\n\t/// CreatedOn:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}\r\n\t/// CreatedBy:Jackie Lee(天宇遊龍)";
        }
    }

    /// <summary>
    /// 字母格式
    /// </summary>
    public enum CharacterFormat
    {
        /// <summary>
        /// 首字母大写
        /// </summary>
        FirstUpper,
        /// <summary>
        /// 首字母小写
        /// </summary>
        FirstLower
    }
}
